package com.ruoyi.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.customer.domain.TCustomerTask;
import com.ruoyi.customer.domain.bo.TCustomerTaskBo;
import com.ruoyi.customer.domain.vo.TCustomerTaskVo;
import com.ruoyi.customer.mapper.TCustomerTaskMapper;
import com.ruoyi.customer.service.ITCustomerTaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 客户签单任务Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-08
 */
@RequiredArgsConstructor
@Service
public class TCustomerTaskServiceImpl implements ITCustomerTaskService {

    private final TCustomerTaskMapper baseMapper;

    /**
     * 查询客户签单任务
     */
    @Override
    public TCustomerTaskVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户签单任务列表
     */
    @Override
    public TableDataInfo<TCustomerTaskVo> queryPageList(TCustomerTaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TCustomerTask> lqw = buildQueryWrapper(bo);
        Page<TCustomerTaskVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户签单任务列表
     */
    @Override
    public List<TCustomerTaskVo> queryList(TCustomerTaskBo bo) {
        LambdaQueryWrapper<TCustomerTask> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TCustomerTask> buildQueryWrapper(TCustomerTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TCustomerTask> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCustomerId() != null, TCustomerTask::getCustomerId, bo.getCustomerId());
        lqw.eq(bo.getManagerId() != null, TCustomerTask::getManagerId, bo.getManagerId());
        lqw.eq(bo.getOrderId() != null, TCustomerTask::getOrderId, bo.getOrderId());
        lqw.eq(bo.getIsNew() != null, TCustomerTask::getIsNew, bo.getIsNew());
        return lqw;
    }

    /**
     * 新增客户签单任务
     */
    @Override
    public Boolean insertByBo(TCustomerTaskBo bo) {
        TCustomerTask add = BeanUtil.toBean(bo, TCustomerTask.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户签单任务
     */
    @Override
    public Boolean updateByBo(TCustomerTaskBo bo) {
        TCustomerTask update = BeanUtil.toBean(bo, TCustomerTask.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TCustomerTask entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户签单任务
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
